//
//  PresentationController.swift
//  PoomsaeScringAll
//
//  Created by Илья Проскурнев on 14/02/2019.
//  Copyright © 2019 Илья Проскурнев. All rights reserved.
//

import UIKit

class PresentationController: UIViewController, UINavigationControllerDelegate, SASRadioButtonDelegate, RACRadioButtonDelegate, EERadioButtonDelegate {
    
    @IBOutlet weak var accyracy: UILabel!
    @IBOutlet weak var presentation: UILabel!
    @IBOutlet weak var total: UILabel!
    
    var idvistuplenie, success: Int!
    var idhost: String!
    var presentationOc: Int!
    var pokaz: Int!
    var vistocen: Int!
    var kort: Int?
    
    var sys: Int!
    
    var sudNum: Int!
    
    var Acd: Double!
    
    var num01: Int!
    
    var num03: Int!
    
    var kolSud: Int!
    
    let URL_OCEN2 = "/php_isap/ocen/ocenki.php"
    
    var pr1 = 20
    var pr2 = 20
    var pr3 = 20
    
    var sasRadioButton: SASRadioButton?
    var racRadioButton: RACRadioButton?
    var eeRadioButton: EERadioButton?
    
    @IBOutlet weak var send: UIButton!
    
    @IBOutlet weak var sas20: UIButton!
    @IBOutlet weak var sas19: UIButton!
    @IBOutlet weak var sas18: UIButton!
    @IBOutlet weak var sas17: UIButton!
    @IBOutlet weak var sas16: UIButton!
    @IBOutlet weak var sas15: UIButton!
    @IBOutlet weak var sas14: UIButton!
    @IBOutlet weak var sas13: UIButton!
    @IBOutlet weak var sas12: UIButton!
    @IBOutlet weak var sas11: UIButton!
    @IBOutlet weak var sas10: UIButton!
    @IBOutlet weak var sas09: UIButton!
    @IBOutlet weak var sas08: UIButton!
    @IBOutlet weak var sas07: UIButton!
    @IBOutlet weak var sas06: UIButton!
    @IBOutlet weak var sas05: UIButton!
    
    @IBOutlet weak var rac20: UIButton!
    @IBOutlet weak var rac19: UIButton!
    @IBOutlet weak var rac18: UIButton!
    @IBOutlet weak var rac17: UIButton!
    @IBOutlet weak var rac16: UIButton!
    @IBOutlet weak var rac15: UIButton!
    @IBOutlet weak var rac14: UIButton!
    @IBOutlet weak var rac13: UIButton!
    @IBOutlet weak var rac12: UIButton!
    @IBOutlet weak var rac11: UIButton!
    @IBOutlet weak var rac10: UIButton!
    @IBOutlet weak var rac09: UIButton!
    @IBOutlet weak var rac08: UIButton!
    @IBOutlet weak var rac07: UIButton!
    @IBOutlet weak var rac06: UIButton!
    @IBOutlet weak var rac05: UIButton!
    
    @IBOutlet weak var ee20: UIButton!
    @IBOutlet weak var ee19: UIButton!
    @IBOutlet weak var ee18: UIButton!
    @IBOutlet weak var ee17: UIButton!
    @IBOutlet weak var ee16: UIButton!
    @IBOutlet weak var ee15: UIButton!
    @IBOutlet weak var ee14: UIButton!
    @IBOutlet weak var ee13: UIButton!
    @IBOutlet weak var ee12: UIButton!
    @IBOutlet weak var ee11: UIButton!
    @IBOutlet weak var ee10: UIButton!
    @IBOutlet weak var ee09: UIButton!
    @IBOutlet weak var ee08: UIButton!
    @IBOutlet weak var ee07: UIButton!
    @IBOutlet weak var ee06: UIButton!
    @IBOutlet weak var ee05: UIButton!
    
    @IBOutlet weak var sas: UILabel!
    @IBOutlet weak var rac: UILabel!
    @IBOutlet weak var ee: UILabel!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        // Do any additional setup after loading the view.

        sasRadioButton = SASRadioButton(buttons: sas20, sas19, sas18, sas17, sas16, sas15, sas14, sas13, sas12, sas11, sas10, sas09, sas08, sas07, sas06, sas05)
        sasRadioButton!.delegate = self
        sasRadioButton!.shouldLetDeSelect = true
        
        racRadioButton = RACRadioButton(buttons: rac20, rac19, rac18, rac17, rac16, rac15, rac14, rac13, rac12, rac11, rac10, rac09, rac08, rac07, rac06, rac05)
        racRadioButton!.delegate = self
        racRadioButton!.shouldLetDeSelect = true
        
        eeRadioButton = EERadioButton(buttons: ee20, ee19, ee18, ee17, ee16, ee15, ee14, ee13, ee12, ee11, ee10, ee09, ee08, ee07, ee06, ee05)
        eeRadioButton!.delegate = self
        eeRadioButton!.shouldLetDeSelect = true
        
        accyracy.text = String(Acd)
        printScoring()
        navigationController?.delegate = self
        
    }
    
    func sasSelectButton(selectedButton: UIButton?)
    {
        sas.text = selectedButton?.currentTitle
        pr1 = Int(Double(selectedButton?.currentTitle ?? "2.0")!*10)
        printScoring()
    }
    
    func racSelectButton(selectedButton: UIButton?)
    {
        rac.text = selectedButton?.currentTitle
        pr2 = Int(Double(selectedButton?.currentTitle ?? "2.0")!*10)
        printScoring()
    }
    
    func eeSelectButton(selectedButton: UIButton?)
    {
        ee.text = selectedButton?.currentTitle
        pr3 = Int(Double(selectedButton?.currentTitle ?? "2.0")!*10)
        printScoring()
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }
    
    func printScoring() {
        let sc = String(Acd)
        let ac = Int(Acd*10)
        accyracy.text = sc
        presentation.text = String(Double(pr1+pr2+pr3)/Double(10))
        total.text = String(Double(pr1+pr2+pr3+ac)/Double(10))
    }
    
    func ocenka_send(){
        self.send.isSelected = false
        presentationOc = pr1+pr2+pr3
        let t = Double (accyracy.text!)! * 10
        let p = Double (presentation.text!)! * 10
        let ocenki_t = Int(t)
        let ocenki_p = Int(p)
        let URL_OCENKI = self.idhost + "/php_isap/ocen/ocenka.php"
        let requestURL = NSURL(string: URL_OCENKI)
        //creating NSMutableURLRequest
        let request = NSMutableURLRequest(url: requestURL! as URL)
        //setting the method to post
        request.httpMethod = "POST"
        //creating the post parameter by concatenating the keys and values from text field
        let postParameters = "vistuplenie_idvistuplenie=" + String (self.idvistuplenie!)+"&ocenki_nomsud=" + String (self.sudNum!) + "&ocenki_t=" + String (ocenki_t) + "&ocenki_p=" + String (ocenki_p);
        //adding the parameters to request body
        request.httpBody = postParameters.data(using: String.Encoding.utf8)
        var myJSON = NSDictionary()
        //creating a task to send the post request
        let task = URLSession.shared.dataTask(with: request as URLRequest){
            data, response, error in
            //parsing the response
            do {
                if (error != nil) {
                    DispatchQueue.main.asyncAfter(deadline: .now() + .seconds(1), execute: {
                        self.ocenka_send()
                    })
                } else {
                //converting resonse to NSDictionary
                   myJSON  =  try JSONSerialization.jsonObject(with: data!, options: JSONSerialization.ReadingOptions.allowFragments) as! NSDictionary;
                    self.success = myJSON["success"] as? Int
                }
            } catch {
                print(error)
            }
            if (self.success != 0) {
                DispatchQueue.main.async {
                    let storyBoard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
                    let nextViewController = storyBoard.instantiateViewController(withIdentifier: "conclusionID") as! ConclusionController
                    nextViewController.Acd = self.Acd
                    nextViewController.idhost = self.idhost
                    nextViewController.idvistuplenie = self.idvistuplenie
                    nextViewController.sudNum = self.sudNum
                    nextViewController.kolSud = self.kolSud
                    nextViewController.presentationOc = self.presentationOc
                    nextViewController.pokaz = self.pokaz
                    nextViewController.sys = self.sys
                    nextViewController.kort = self.kort
                    nextViewController.vistocen = self.vistocen
                    self.navigationController?.pushViewController(nextViewController, animated: true)
                }
            }
        }
        
        task.resume()
        
    }
    
    @IBAction func backItem(_ sender: UIButton) {
        self.navigationController?.popViewController(animated: true)
    }
    
    @IBAction func send(_ sender: UIButton) {
        ocenka_send()
    }
    
//    func navigationController(_ navigationController: UINavigationController, willShow viewController: ViewController, animated: Bool) {
//        if (vistocen == 2) {
//            _ = (viewController as? OcenController)?.Acd
//            _ = (viewController as? OcenController)?.num01
//            _ = (viewController as? OcenController)?.num03
//            _ = (viewController as? OcenController)?.idhost
//            _ = (viewController as? OcenController)?.idvistuplenie
//            _ = (viewController as? OcenController)?.sudNum
//            _ = (viewController as? OcenController)?.pokaz
//            _ = (viewController as? OcenController)?.kort
//        }
//        if (vistocen == 1) {
//            _ = (viewController as? ButtonOcenController)?.Acd
//            _ = (viewController as? ButtonOcenController)?.num01
//            _ = (viewController as? ButtonOcenController)?.num03
//            _ = (viewController as? ButtonOcenController)?.idhost
//            _ = (viewController as? ButtonOcenController)?.idvistuplenie
//            _ = (viewController as? ButtonOcenController)?.sudNum
//            _ = (viewController as? ButtonOcenController)?.pokaz
//            _ = (viewController as? ButtonOcenController)?.kort
//        }
//    }
    
}
